package com.bnyy.medicalHousekeeper.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.bean.Article;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/8
 **/
public class ArticleAdapter extends BaseNormalListAdapter<Article, ArticleAdapter.ViewHolder> {

    private SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd");

    public ArticleAdapter(Context context, OnItemClickListener<Article> onItemViewClickListener) {
        super(context, onItemViewClickListener);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_article, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        Article article = getData(position);
        holder.tvArticleTitle.setText(article.getArticle_title());

        GlideHelper.setRoundImage(mContext, article.getArticle_image(), holder.ivArticle);

        if (holder.tvType.getBackground() == null) {
            GradientDrawable bg = new GradientDrawable();
            bg.setColor(Color.parseColor("#80000000"));
            bg.setCornerRadius(360);
            holder.tvType.setBackground(bg);
        }
        holder.tvType.setText(article.getType_desc());

        holder.tvDate.setText("");
        try {
            String createTime = article.getCreate_time();
            if (!TextUtils.isEmpty(createTime)) {
                String s = createTime.split(" ")[0];
                Date date = sdfYMD.parse(s);
                if (date != null) {
                    if (DateUtils.isToday(date.getTime())) {
                        holder.tvDate.setText("今天");
                    } else {
                        holder.tvDate.setText(s);
                    }
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        TextView tvArticleTitle;
        ImageView ivArticle;
        TextView tvType;
        TextView tvDate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvArticleTitle = itemView.findViewById(R.id.tv_article_title);
            ivArticle = itemView.findViewById(R.id.iv_article);
            tvType = itemView.findViewById(R.id.tv_type);
            tvDate = itemView.findViewById(R.id.tv_date);
        }
    }
}
